package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.BusModel;
import com.ruoyi.system.service.IBusModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProductGroup;
import com.ruoyi.system.service.IProductGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品分类Controller
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/product/group")
public class ProductGroupController extends BaseController
{
    @Autowired
    private IProductGroupService productGroupService;
    @Autowired
    private IBusModelService busModelService;

    /**
     * 查询产品分类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductGroup productGroup)
    {
        startPage();
        List<ProductGroup> list = productGroupService.selectProductGroupList(productGroup);
        return getDataTable(list);
    }

    /**
     * 导出产品分类列表
     */
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductGroup productGroup)
    {
        List<ProductGroup> list = productGroupService.selectProductGroupList(productGroup);
        ExcelUtil<ProductGroup> util = new ExcelUtil<ProductGroup>(ProductGroup.class);
        util.exportExcel(response, list, "产品分类数据");
    }

    /**
     * 获取产品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productGroupService.selectProductGroupById(id));
    }

    /**
     * 新增产品分类
     */
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductGroup productGroup)
    {
        return toAjax(productGroupService.insertProductGroup(productGroup));
    }

    /**
     * 修改产品分类
     */
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductGroup productGroup)
    {
        return toAjax(productGroupService.updateProductGroup(productGroup));
    }

    /**
     * 删除产品分类
     */
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productGroupService.deleteProductGroupByIds(ids));
    }


    @RequestMapping("/getGroupTree")
    public AjaxResult getGroupTree(){
        List<HashMap<String,Object>> list = new ArrayList<>();
         //第一级-产品分类
        HashMap<String,Object> productMap = new HashMap<>();
        productMap.put("code","");
        productMap.put("name","产品");
        //第二级-开票类型/九大类
        HashMap<String,Object> invoiceMap = new HashMap<>();
        invoiceMap.put("code","");
        invoiceMap.put("name","开票类信息");
        //开票类型四种写死
        List<HashMap<String,Object>> invoceGroup = new ArrayList<>();
        HashMap<String,Object> hashMap1 = new HashMap<>();
        hashMap1.put("code","");
        hashMap1.put("name","配件");
        invoceGroup.add(hashMap1);
        HashMap<String,Object> hashMap2 = new HashMap<>();
        hashMap2.put("code","");
        hashMap2.put("name","制冷剂");
        invoceGroup.add(hashMap2);
        HashMap<String,Object> hashMap3 = new HashMap<>();
        hashMap3.put("code","");
        hashMap3.put("name","轮胎");
        invoceGroup.add(hashMap3);
        HashMap<String,Object> hashMap4 = new HashMap<>();
        hashMap4.put("code","");
        hashMap4.put("name","润滑油");
        invoceGroup.add(hashMap4);
        invoiceMap.put("childList",invoceGroup);

        //添加九大类
        HashMap<String,Object> nineMap = new HashMap<>();
        nineMap.put("code","");
        nineMap.put("name","九大类");

        //添加车型大类
        HashMap<String,Object> modelMap = new HashMap<>();
        modelMap.put("code","");
        modelMap.put("name","车型");

        //九大类子类
        List<HashMap<String,Object>> nineMapList = new ArrayList<>();
         List<ProductGroup> firstGroup = productGroupService.listFirstGroup();
        for (ProductGroup productGroup:firstGroup) {
            HashMap<String,Object> firstMap = new HashMap<>();
            firstMap.put("code",productGroup.getCode());
            firstMap.put("name",productGroup.getName());
            ProductGroup firstPro = new  ProductGroup();
            firstPro.setParentId(productGroup.getId().toString());
            List<ProductGroup> secondGroup = productGroupService.selectProductGroupList(firstPro);
            List<HashMap<String,Object>> secondMapList = new ArrayList<>();
            if(secondGroup!=null&&secondGroup.size()>0){
                for (ProductGroup productGroupSecond:secondGroup) {
                    HashMap<String,Object> secondMap = new HashMap<>();
                    secondMap.put("code",productGroupSecond.getCode());
                    secondMap.put("name",productGroupSecond.getName());
                    ProductGroup secondPro = new  ProductGroup();
                    secondPro.setParentId(productGroupSecond.getId().toString());
                    List<ProductGroup> threeGroup = productGroupService.selectProductGroupList(secondPro);
                    secondMap.put("childList",threeGroup);
                    secondMapList.add(secondMap);
                }
            }
            firstMap.put("childList",secondMapList);
            nineMapList.add(firstMap);
        }
        //添加九大类map
        nineMap.put("childList",nineMapList);

        //车型类子类
        List<HashMap<String,Object>> modelMapList = new ArrayList<>();
        List<BusModel> busModels = busModelService.selectBusModelList(new BusModel());
        for (BusModel busModel:busModels) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",busModel.getId());
        map.put("name",busModel.getBusModel());
        modelMapList.add(map);
        }
        modelMap.put("childList",modelMapList);


        List<HashMap<String,Object>> returnList = new ArrayList<>();
        returnList.add(invoiceMap);
        returnList.add(nineMap);
        returnList.add(modelMap);
        productMap.put("childList",returnList);
        list.add(productMap);
        return AjaxResult.success(list);
    }





}
